package com.syni.mdd.sjb.common.datatreaure.service.impl.databank.saasminapp;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.utils.common.utils.*;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.saasminapp.coupon.IdRqVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.saasminapp.coupon.SaveCouponRqVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.saasminapp.coupon.TypeRqVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.saasminapp.coupon.UpdateCouponRqVo;
import com.syni.mdd.sjb.common.datatreaure.service.databank.saasminapp.MinCouponService;
import com.syni.mdd.sjb.common.component.utils.feign.service.mdd.commonuser.UserRestService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Log4j2
public class MinCouponServiceImpl implements MinCouponService {
    @Autowired
    private UserRestService userRestService;

    @Override
    public Map<String, Object> stopPublishCoupon(IdRqVo idRqVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.sms.service") + "marking/SmsCoupon/stopPublish/" + idRqVo.getId();
            Map<String, Object> headMap = new HashMap<>();
            headMap.put("storeId",idRqVo.getStoreId());
            Map<String, Object> resultMap = postReqMember(postUrl, idRqVo.getToken(), idRqVo.getUserId(), true,headMap,new HashMap<>());
            return resultMap;
        }catch (Exception e){
            log.info(e.getMessage(),e);
        }
        return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }

    @Override
    public Map<String, Object> getCouponList(TypeRqVo typeRqVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.sms.service") + "marking/SmsCoupon/list";
            Map<String,Object> params = new HashMap<>();
            params.put("type",typeRqVo.getType());
            Map<String, Object> headMap = new HashMap<>();
            headMap.put("storeId",typeRqVo.getStoreId());
            Map<String, Object> resultMap = getReqWithHead(postUrl, typeRqVo.getToken(), typeRqVo.getUserId(), true,headMap,params);
            return resultMap;
        }catch (Exception e){
            log.info(e.getMessage(),e);
        }
        return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }

    @Override
    public Map<String, Object> saveCoupon(SaveCouponRqVo saveCouponRqVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.sms.service") + "marking/SmsCoupon/create";
            Map<String,Object> headMap = new HashMap<>();
            headMap.put("storeId",saveCouponRqVo.getStoreId());
            Map<String,Object> params = BeanUtils.objectToMap(saveCouponRqVo);
            Map<String, Object> resultMap = postReqMember(postUrl, saveCouponRqVo.getToken(), saveCouponRqVo.getUserId(),true,headMap, params);
            return resultMap;
        }catch (Exception e){
            log.info(e.getMessage(),e);
        }
        return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }

    @Override
    public Map<String, Object> updateCoupon(UpdateCouponRqVo updateCouponRqVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.sms.service") + "marking/SmsCoupon/update/" + updateCouponRqVo.getId();
            Map<String, Object> headMap = new HashMap<>();
            headMap.put("storeId",updateCouponRqVo.getStoreId());
            Map<String,Object> params = BeanUtils.objectToMap(updateCouponRqVo);
            Map<String, Object> resultMap = postReqMember(postUrl, updateCouponRqVo.getToken(), updateCouponRqVo.getUserId(),true,headMap, params);
            return resultMap;
        }catch (Exception e){
            log.info(e.getMessage(),e);
        }
        return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }

    @Override
    public Map<String, Object> getCouponDetail(IdRqVo idRqVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.sms.service") + "marking/SmsCoupon/" + idRqVo.getId();
            Map<String, Object> headMap = new HashMap<>();
            headMap.put("storeId",idRqVo.getStoreId());
            Map<String, Object> resultMap = getReqWithHead(postUrl, idRqVo.getToken(), idRqVo.getUserId(),true,headMap,new HashMap<>());
            return resultMap;
        }catch (Exception e){
            log.info(e.getMessage(),e);
        }
        return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }

    @Override
    public Map<String, Object> deleteCoupon(IdRqVo idRqVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.sms.service") + "marking/SmsCoupon/delete/" + idRqVo.getId();
            Map<String, Object> headMap = new HashMap<>();
            headMap.put("storeId",idRqVo.getStoreId());
            Map<String, Object> resultMap = getReqWithHead(postUrl, idRqVo.getToken(), idRqVo.getUserId(),true,headMap,new HashMap<>());
            return resultMap;
        }catch (Exception e){
            log.info(e.getMessage(),e);
        }
        return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }

    public Map<String,Object> getReqWithHead(String requestUrl,String token,Integer userId,Boolean isExtra,Map<String,Object> extraMap,Map<String,Object> params){
        Map<String,Object> resultMap = null;
        try {
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(userId,token);
            log.info("=====rpcResponseVo======" + rpcResponseVo);
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                Map<String, Object> saasHeader = RestTemplateUtils.getSassHeader(minitoken);
                if(isExtra){
                    saasHeader.putAll(extraMap);
                }
                String reponseBody = RestTemplateUtils.getForEntityWithHead(requestUrl,params,saasHeader);
                resultMap = JSON.parseObject(reponseBody,Map.class);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return resultMap;
    }


    public Map<String,Object> reqMember(String requestUrl,String token,Integer userId,Map<String,Object> params){
        Map<String,Object> resultMap = null;
        try {
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(userId,token);
            log.info("=====rpcResponseVo======" + rpcResponseVo);
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                String reponseBody = RestTemplateUtils.getForEntityWithHead(requestUrl,params,RestTemplateUtils.getSassHeader(minitoken));
                resultMap = JSON.parseObject(reponseBody,Map.class);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return resultMap;
    }

    public Map<String,Object> postReqMemberWithToken(String requestUrl,String minToken,Integer userId,
                                            Boolean isExtra,Map<String,Object> extraMap,Map<String,Object> params){
        Map<String,Object> resultMap = null;
        try {
            Map<String, Object> saasHeader = RestTemplateUtils.getSassHeader(minToken);
            if(isExtra){
                saasHeader.putAll(extraMap);
            }
            String reponseBody = RestTemplateUtils.postForEntityForJsonByLoadBalanced(requestUrl,params,saasHeader);
            resultMap = JSON.parseObject(reponseBody,Map.class);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return resultMap;
    }

    public Map<String,Object> postReqMember(String requestUrl,String token,Integer userId,
                                Boolean isExtra,Map<String,Object> extraMap,Map<String,Object> params){
        Map<String,Object> resultMap = null;
        try {
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(userId,token);
            log.info("=====rpcResponseVo======" + rpcResponseVo);
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                Map<String, Object> saasHeader = RestTemplateUtils.getSassHeader(minitoken);
                if(isExtra){
                    saasHeader.putAll(extraMap);
                }
                String reponseBody = RestTemplateUtils.postForEntityForJsonByLoadBalanced(requestUrl,params,saasHeader);
                resultMap = JSON.parseObject(reponseBody,Map.class);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return resultMap;
    }

    public Map<String,Object> postFormReqMember(String requestUrl,String token,Integer userId,
                                Boolean isExtra,Map<String,Object> extraMap,Map<String,Object> params){
        Map<String,Object> resultMap = null;
        try {
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(userId,token);
            log.info("=====rpcResponseVo======" + rpcResponseVo);
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                Map<String, Object> saasHeader = RestTemplateUtils.getSassHeader(minitoken);
                if(isExtra){
                    saasHeader.putAll(extraMap);
                }
                String reponseBody = RestTemplateUtils.postForEntity(RestTemplateUtils.getLoadRestTemplate(),requestUrl,params,saasHeader);
                resultMap = JSON.parseObject(reponseBody,Map.class);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return resultMap;
    }


}
